package com.ponhu.ea2.stockcenter.provider.schedule;

import com.alibaba.fastjson.JSON;
import com.ponhu.ea2.stockcenter.provider.constants.CommonConstant;
import com.ponhu.ea2.stockcenter.provider.entity.StockChangeMqLogs;
import com.ponhu.ea2.stockcenter.provider.service.entity.StockChangeMqLogsService;
import com.ponhu.ea2.stockcenter.provider.support.service.RabbitMQService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class StockChangeMqTask {

    @Autowired
    private RabbitMQService rabbitMQService;
    @Autowired
    private StockChangeMqLogsService stockChangeMqLogsService;

    /**
     * 货品库存变动MQ定时补推任务
     */
    @XxlJob("stockChangeFailMqTask")
    public void stockChangeFailMqTask() {

        List<String> inventoryCodes = stockChangeMqLogsService.getListByStatus(CommonConstant.SEND_MQ_STATUS_0)
                .stream().map(StockChangeMqLogs::getInventoryCode).distinct().collect(Collectors.toList());

        if (!stockChangeMqLogsService.updStatusByInventoryCodes(inventoryCodes, CommonConstant.SEND_MQ_STATUS_2))
            log.error("货品库存变动MQ定时补推任务修改发送日志状态失败 ===>> {}", JSON.toJSONString(inventoryCodes));

        rabbitMQService.saleStockChangeSendMsg(inventoryCodes);
    }


}
